using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Threading;

namespace HW2and3
{
    class NQModelBFS : ModelBase
    {
        private ArrayList observers = new ArrayList();

        private Thread modelThread;

        volatile private int sleepInterval;
        volatile private bool isSuspended;
        volatile private bool isStoped;

        public NQModelBFS() { }

        public override void Initialize(int sleepInterval, int size, int x, int y)
        {
            base.Initialize(sleepInterval, size, x, y);

            modelThread = new Thread(this.BFSStart);
        }

        private void BFSStart()
        {
            NQStateQueen state = new NQStateQueen(size, x, y, 0);
            BFS(state);
        }

        private bool BFS(NQStateQueen state)
        {
            Queue queue = new Queue();
            queue.Enqueue(state);
            while (queue.Count > 0)
            {
                state = (NQStateQueen)queue.Dequeue();
                if (state.Index == size * size)
                    return true;
                for(int i = 0; i < size; i++)
                    for (int j = 0; j < size; j++)
                        if (state.board[i, j] == 0) queue.Enqueue(state.GetNewState(i, j));
            }
            return false;
        }
    }
}
